package com.kun.dao.role;

import com.kun.dao.BaseDao;
import com.kun.entity.Role;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class RoleDaoImpl implements RoleDao {

    //获取角色信息列表
    public ArrayList<Role> getRoleList(Connection connection) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        ArrayList<Role> list = new ArrayList<Role>();

        if(connection != null){
            String sql = "SELECT * FROM smbms_role";
            rs = BaseDao.executeQuery(connection,ps,rs,sql,null);

            while (rs.next()){
                Role role = new Role();
                role.setCreatedBy(rs.getInt("createdBy"));
                role.setCreationDate(rs.getDate("creationDate"));
                role.setId(rs.getLong("id"));
                role.setModifyBy(rs.getInt("modifyBy"));
                role.setModifyDate(rs.getDate("modifyDate"));
                role.setRoleCode(rs.getString("roleCode"));
                role.setRoleName(rs.getString("roleName"));
                list.add(role);
            }

            BaseDao.closeResource(ps,rs);

        }

        return list;
    }

    //根据角色id获取角色名
    public String getRoleName(Connection connection, Integer roleId)throws SQLException{
        PreparedStatement ps = null;
        ResultSet rs = null;
        String roleName = "";
        if(connection != null && roleId != null){
            String sql = "SELECT roleName FROM smbms_role WHERE id = ?;";
            rs = BaseDao.executeQuery(connection,ps,rs,sql,new Object[]{roleId});
            if(rs.next()){
                roleName = rs.getString("roleName");
            }
            BaseDao.closeResource(ps,rs);
        }
        return roleName;
    }
}
